package cn.lhx.blog.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;

import cn.lhx.blog.pojo.Article;

/**
 * 文章类Dao对象
 * @author lhx
 *
 */
public class DaoArticle extends BasicDao{

	@Override
	public int delete(int id) {
		// 获得改ID的日志实体
		Article article = (Article)getHibernateTemplate().get(
				Article.class, id);
		// 删除它
		getHibernateTemplate().delete(article);
		return id;
	}

	@Override
	public List<?> executeQuery(String sql) {
		return this.executeQuery(sql, 0);
	}
	public List<?> executeQuery(String sql,int type) {
		//if(HQL_TYPE)
		Query query =getSessionFactory().getCurrentSession().createQuery(sql);
		List list=query.list();
		return list;
	}

	@Override
	public int executeUpdate(String sql) {
		return this.executeUpdate(sql, 0);
	}
	public int executeUpdate(String sql,int type) {
		Query query =getSessionFactory().getCurrentSession().createQuery(sql);
		return query.executeUpdate();
	}

	@Override
	public Object getById(int id) {
		return (Article)getHibernateTemplate().get(Article.class, id);
	}

	@Override
	public List<Article> getList(int begin, int size) {
		List<Article> articles = new ArrayList<Article>();
		Query query = getSessionFactory().getCurrentSession()
				.createQuery("from Article article order by article.postTime desc");
		query.setFirstResult(begin);
		query.setMaxResults(size);
		articles = query.list();
		return articles;
	}

	@Override
	public Object insert(Object obj) {
		Article article=(Article)obj;
		getHibernateTemplate().save(article);
		return article;
	}

	@Override
	public Object update(Object obj) {
		Article article=(Article)obj;
		getHibernateTemplate().update(article);
		return article;
	}
	
}
